﻿using System;
using System.IO;
using System.Text;


namespace InformaticaDiary.Common
{   
    public class ExceptionLogger : ApplicationException
    {  
        private static string FileNLogStr;
        private static string FileNExcStr;

        private static string _ApplicationStartupPath = string.Empty;

        public static string ApplicationStartupPath
        {            
            set { 
                ExceptionLogger._ApplicationStartupPath = value;
                if (Directory.Exists(_ApplicationStartupPath + "\\Log") == false)
                    Directory.CreateDirectory(_ApplicationStartupPath + "\\Log");
                FileNLogStr = _ApplicationStartupPath + "\\Log\\" + String.Format("UI{0:dd-MM-yyyy}.log", DateTime.Today);
                FileNExcStr = _ApplicationStartupPath + "\\Log\\" + String.Format("Trace{0:dd-MM-yyyy}.log", DateTime.Today);
            }
        }

        delegate void MyDelegate(String exception, Exception ex, DateTime dt);
        delegate void MyDelegatestr(String exception, DateTime dt);

        static ExceptionLogger()
        {     
            
           
        }
        public ExceptionLogger(string message, Exception innerException)
            : base(message, innerException)
        {
        }
       

        // Function to write the information about the TeaBoardException
        // I/P parameters : Exception object
        // O/P parameter : bool value indicating status of the WriteLog operation.

        public static void WriteUIError(String exception, Exception ex)
        {
            MyDelegate del = new MyDelegate(WriteUIErrorDel);
            del.BeginInvoke(exception, ex, DateTime.Now, null, null);
        }
        public static void WriteUIErrorDel(String exception, Exception ex, DateTime dt)
        {
            StringBuilder LogBuilder = new StringBuilder();           
            try
            {                 
                LogBuilder.Append(String.Format("{0:dd/MM/yyyy HH:mm:ss.fff} ,", dt));
                System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
                LogBuilder.Append(exception);
                LogBuilder.Append("MethodName: " + trace.GetFrame(0).GetMethod().Name + ", LineNo: " + trace.GetFrame(0).GetFileLineNumber() + ", Column: " + trace.GetFrame(0).GetFileColumnNumber() + ", " +
                "Message: " + ex.Source + ", " + ex.Message);
                lock (FileNLogStr)
                {
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(FileNLogStr, true))
                    {
                        file.WriteLine(LogBuilder);
                    }
                }               
            }
            catch (Exception exp)
            {

            }
        }

        // Function to write the information about the TeaBoardException
        // I/P parameters : Exception object
        // O/P parameter : bool value indicating status of the WriteLog operation.

        public static void WriteUIError(String exception)
        {
            MyDelegatestr del = new MyDelegatestr(WriteUIErrorStrDel);
            del.BeginInvoke(exception, DateTime.Now, null, null);
        }
        public static void WriteUIErrorStrDel(String exception, DateTime dt)
        {
            StringBuilder LogBuilder = new StringBuilder();            
            try
            {        
                LogBuilder.Append(String.Format("{0:dd/MM/yyyy HH:mm:ss.fff} ,", dt));
                LogBuilder.Append(exception);
                lock (FileNLogStr)
                {
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(FileNLogStr, true))
                    {
                        file.WriteLine(LogBuilder);
                    }
                }               
            }           
            catch (Exception ex)
            {
            }
        }

        public static void WriteTrace(string exception)
        {
            MyDelegatestr del = new MyDelegatestr(WriteTraceDel);
            del.BeginInvoke(exception, DateTime.Now, null, null);
        }
        public static void WriteTraceDel(string exception, DateTime dt)
        {
            StringBuilder LogBuilder = new StringBuilder();           
            try
            { 
                LogBuilder.Append(String.Format("{0:dd/MM/yyyy HH:mm:ss.fff} ,", dt));
                LogBuilder.Append(exception);
                lock (FileNExcStr)
                {
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(FileNExcStr, true))
                    {
                        file.WriteLine(LogBuilder);
                    }
                }
            }
            catch (ExceptionLogger ptEx)
            {
            }
            catch (Exception ex)
            {
            }
        }
    }

    public class ValidationException : Exception
    {
        public ValidationException(string message)
            : base(message)
        {

        }
    }
   
}
